﻿/*
 *    Copyright © 2012 FPInst authors
 *
 *      This file is part of the FPInst project 
 *      (for more information, see: http://code.google.com/p/fpinst/).
 *
 *    FPInst is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    FPInst is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with FPInst.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SignalGen
{
    class Waveform
    {

        public enum WaveType
        {
            Sinus,
            Cosinus,
            Triangle,
            Square,
            Noise,
            Null
        }

        public static byte[] Generate(WaveType type, int xsize, double ysize, double duty)
        {
            byte[] ret = new byte[xsize];
            Random r = new Random();

            int i = 0;
            double halfsize = xsize * (duty / 100);
            double step = Math.PI / (halfsize);
            for (; i < halfsize; i++)
            {
                switch (type)
                {
                    case WaveType.Sinus:
                        ret[i] = (byte)((ysize / 2) + ((ysize / 2 -1) * (0-Math.Sin(step * i))));
                        break;
                    case WaveType.Cosinus:
                        ret[i] = (byte)((ysize / 2) + ((ysize / 2 -1) * (0-Math.Cos(step * i))));
                        break;
                    case WaveType.Triangle:
                        ret[i] = (byte)(ysize -(ysize * (i * (1/halfsize))));
                        break;
                    case WaveType.Square:
                        ret[i] = (byte)1;
                        break;
                    case WaveType.Noise:
                        ret[i] = (byte)r.Next((int)ysize);
                        break;
                    case WaveType.Null:
                        ret[i] = (byte)(ysize / 2);
                        break;
                }
            }

            halfsize = xsize - halfsize;
            step = Math.PI / (halfsize);
            int j = 0;
            for (; i < xsize; i++)
            {
                switch (type)
                {
                    case WaveType.Sinus:
                        ret[i] = (byte)((ysize / 2) + ((ysize / 2) * (0-Math.Sin(Math.PI + step * j))));
                        break;
                    case WaveType.Cosinus:
                        ret[i] = (byte)((ysize / 2) + ((ysize / 2) * (0-Math.Cos(Math.PI + step * j))));
                        break;
                    case WaveType.Triangle:
                        ret[i] = (byte)(ysize * (j * (1/halfsize)));
                        break;
                    case WaveType.Square:
                        ret[i] = (byte)ysize;
                        break;
                    case WaveType.Noise:
                        ret[i] = (byte)r.Next((int)ysize);
                        break;
                    case WaveType.Null:
                        ret[i] = (byte)(ysize / 2);
                        break;
                }
                j++;
            }


            return ret;
        }

    }
}
